//
//  MXFindSearchDetailVC.m
//  MXの衣柜
//
//  Created by 马霄 on 15/8/18.
//  Copyright (c) 2015年 霄 马. All rights reserved.
// 专题搜索详情页
#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "MXFindSearchDetailVC.h"
#import "AFNetworkHandler.h"
#import "MXFindModel.h"
#import "MXFindContentCollectionViewCell.h"
#import "MXFindDetailVC.h"
#import "MBProgressHUD.h"
@interface MXFindSearchDetailVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;

@property (nonatomic, retain) NSMutableArray *topicArray;

@property (nonatomic, retain) MBProgressHUD *HUD;

@property (nonatomic, retain) UIView *alterView;

@property (nonatomic, retain) UILabel *reportLabel;
@end

@implementation MXFindSearchDetailVC
- (void)dealloc
{
    [_reportLabel release];
    [_HUD release];
    [_alterView release];
    [_topicArray release];
    [_searchResult release];
    [_collectionView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    // 全局数组初始化
    self.topicArray = [NSMutableArray array];
    
    self.reportLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 50)];
    self.reportLabel.center = self.view.center;
    self.reportLabel.font = [UIFont boldSystemFontOfSize:20];
    self.reportLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.reportLabel];
    
    
// 判断是否有数据
    if ([self.searchResult isEqualToString:@""]) {
        
        self.reportLabel.text = @"无相关信息";
        
    } else if (self.searchResult.length > 10){
        
        self.reportLabel.text = @"残念~~, 无相关信息";

    } else if (!self.topicArray){
        
        self.reportLabel.text = @"残念~~, 无相关信息";
        
    } else {
        self.reportLabel.hidden = YES;
        
        [self getData];
    }
    
    
    
    
//    [self getData];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHTPRO * 64)];
    view.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(WIDTHPRO * 130, HEIGHTPRO * 24, WIDTHPRO * 120, HEIGHTPRO * 40)];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.text = @"搜索结果";
    [titleLabel setTextColor:[UIColor blueColor]];
    [view addSubview:titleLabel];
    [self.view addSubview:view];
    
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(WIDTHPRO * 10, HEIGHTPRO * 29, WIDTHPRO * 30, HEIGHTPRO * 30);
    [backButton setBackgroundImage:[UIImage imageNamed:@"search_back_blue"] forState:(UIControlStateNormal)];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    [view addSubview:backButton];

    
    // Do any additional setup after loading the view.
}

-(void)uiActivityindicatorviewStart
{
    self.alterView =[[[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)] autorelease];
    self.alterView.backgroundColor =[UIColor whiteColor];
    self.HUD = [MBProgressHUD showHUDAddedTo:self.alterView animated:YES];
//    self.HUD.labelText = @"正在努力的给您加载";
    [self.HUD show:YES];
    
    [self.view addSubview:self.alterView];
    
}
-(void)uiActivityindicatorviewEnd
{
    [self.alterView removeFromSuperview];
}
#pragma mark - FindTabBar设置CollectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *flowLayout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    flowLayout.itemSize = CGSizeMake(WIDTHPRO * 170,HEIGHTPRO * 210);
    flowLayout.minimumLineSpacing = 10;
    flowLayout.sectionInset = UIEdgeInsetsMake(WIDTHPRO * 10 , HEIGHTPRO * 10 , 0, HEIGHTPRO * 10);
    
    self.collectionView = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHTPRO * 64, WIDTH, HEIGHT - HEIGHTPRO * 64) collectionViewLayout:flowLayout] autorelease];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.collectionView];
    
    
    [self.collectionView registerClass:[MXFindContentCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];
        
    [self uiActivityindicatorviewStart];
    
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(uiActivityindicatorviewEnd) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

    
}
/************************* 搜素部分数据解析 **************************/
- (void)getData
{
    NSString *Path = [NSString stringWithFormat:@"http://rm.app.hongniujia.com/json_rm.php?action=list&ver=10&page=0&category=nanren&category2=%%2F%@&pageSize=24&appCookie=channel_AppStore%%20version_6.11%%20oid_AE53CF37-EAEB-468C-8507-15A922FDD348%%20package_com.aslibra.rm.nanren%%20platform_iphone%%20ver_8.4.1%%20jb_0%%20t1_1439897262%20md5_89%%20t_1438671553&width=320&keyword=",self.searchResult];
    NSLog(@"%@", Path);
    [AFNetworkHandler GETwithUTF8AFNByURL:Path completion:^(id result) {
        NSLog(@"result = %@", result);
        
        NSArray *dataArr = [result objectForKey:@"data"];
        for (NSDictionary *dict in dataArr) {
            MXFindModel *model = [[[MXFindModel alloc] initWithDic:dict] autorelease];
            
            [self.topicArray addObject:model];
        }
        
        
// 不是动态请求的数据 不需要relodata
        
        if (self.topicArray == nil) {
            self.reportLabel.text = @"残念~~, 无相关信息";

        } 
        [self creatCollectionView];
        NSLog(@"%@", self.topicArray);
        [self uiActivityindicatorviewEnd];
    }];
}



/************************* CollectionView Delegate **************************/

//Collection中的组别个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个组别中元件的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.topicArray.count;
}

//重用机制
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindContentCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    
    cell.layer.cornerRadius = 8;
    
    cell.layer.masksToBounds = YES;
    
    cell.modelS = self.topicArray[indexPath.row];
    
    return cell;
}


//collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MXFindDetailVC *detailVC = [[[MXFindDetailVC alloc] init] autorelease];
    MXFindModel *model = self.topicArray[indexPath.row];
    detailVC.AlbumID = [self.topicArray[indexPath.row] albumId];
    detailVC.TypeID = [self.topicArray[indexPath.row] album_type];
    detailVC.TempImage = model.img;
    detailVC.TempTitleString = model.title;
    [self.navigationController pushViewController:detailVC animated:YES];
}


/*
//设置HeaderView
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *topView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
    
        [topView addSubview:view];
    return topView;
}
*/

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
